const {
  MemberModel
} = require('../../../../utils/member-model.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['博士', '硕士', '本科', '大专', '高中', '初中', '其他'],
    familyInfoItem: ['我还小', '单身狗', '热恋中', '小两口', '宝宝爸', '宝宝妈'],
    region: [], //常住城市
    memberName: "", //会员名称
    memberImg: "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
    gender: "", //性别 0:男 1:女
    birthday: "", //生日
    idNumber: "", //身份证
    education: "", //教育背景
    email: "", //绑定邮箱
    familyInfo: "", //家庭信息
    keyUpdate: "", //修改的键
    valueUpdate: "", //修改的值
    integral: {}, //积分信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo(); //微信会员信息
    this.getUserActivityInfo(); //获取完善资料活动积分配置
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      var cityValue = data.city ? JSON.parse(data.city) : [];
      that.setData({
        memberName: data.memberName || "", //会员名称
        memberImg: data.avatarUrl || "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
        gender: data.gender == 0 ? "男" : data.gender == 1 ? "女" : "", //性别 0:男 1:女
        birthday: data.birthday ? data.birthday.substr(0, 11) : "", //生日
        idNumber: data.idNumber ? data.idNumber : "", //身份证
        education: data.education ? data.education : "", //教育背景
        region: cityValue, //常住城市
        email: data.email ? data.email : "", //绑定邮箱
        familyInfo: data.familyInfo ? data.familyInfo : "", //家庭信息
        tags: data.tags ? data.tags.replace(/,/g, "/") : "", //标签
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 个人信息设置
   */
  goModifyInformation(event) {
    var type = event.currentTarget.dataset.type; //修改的key
    var value = event.currentTarget.dataset.value; //修改的值
    if (type != "birthday" || value == "") {
      wx.navigateTo({
        url: '/pages/memberCenter/information/modifyInformation/index?keyType=' + type + "&keyValue=" + value,
      })
    }
  },

  /**
   * 我的标签
   */
  goIndividualLabel() {
    wx.navigateTo({
      url: '/pages/memberCenter/information/individualLabel/index',
    })
  },

  /**
   * 选择教育背景
   */
  bindPickerChange: function (event) {
    var type = event.currentTarget.dataset.type;
    this.setData({
      keyUpdate: type,
      valueUpdate: event.detail.value,
      education: event.detail.value
    }, function () {
      this.modifyPersonalInformation();
    })
  },
  /**
   * 选择常住城市
   */
  bindRegionChange: function (event) {
    var type = event.currentTarget.dataset.type;
    this.setData({
      keyUpdate: type,
      valueUpdate: JSON.stringify(event.detail.value),
      region: event.detail.value
    }, function () {
      this.modifyPersonalInformation();
    })
  },

  /**
   * 家庭信息
   */
  bindfamilyInfoChange: function (event) {
    var type = event.currentTarget.dataset.type;
    this.setData({
      keyUpdate: type,
      valueUpdate: event.detail.value,
      familyInfo: event.detail.value
    }, function () {
      this.modifyPersonalInformation();
    })
  },

  /**
   * 修改个人信息
   * key memberName(姓名),gender(性别),birthday(生日),idNumber(身份证),education(教育背景),city(常住城市),email(绑定邮箱),familyInfo(家庭信息)
   */
  modifyPersonalInformation: function () {
    var param = {}
    param.merchantId = wx.getStorageSync('merchantId');
    param.key = this.data.keyUpdate;
    param.value = this.data.valueUpdate;
    memberModel.userUpdate(param).then(data => {
      wx.showToast({
        title: `编辑成功！`,
        icon: 'none',
        duration: 2000
      })
      this.setData({
        keyUpdate: "",
        valueUpdate: "",
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 获取完善资料活动积分配置
   */
  getUserActivityInfo: function () {
    memberModel.userActivityInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      this.setData({
        integral: data || {}
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  }
})